package com.tnovoselec.android.mojevijesti.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.widget.ShareActionProvider;
import com.tnovoselec.android.mojevijesti.R;
import com.tnovoselec.android.mojevijesti.adapter.NewsDetailsAdapter;
import com.tnovoselec.android.mojevijesti.dto.NewsResultHolder;

public class NewsDetailsFragment extends SherlockFragment implements OnPageChangeListener {
	
	public interface NewsItemChangedListener{
		public void onNewsItemChanged(int position);
	}

	private ViewPager viewpager;
	private NewsDetailsAdapter adapter;

	int channel;
	int position;

	private NewsItemChangedListener listener;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		channel = getArguments().getInt("channel");
		position = getArguments().getInt("position");

		ActionBar actionBar = getSherlockActivity().getSupportActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setTitle(NewsResultHolder.getTitleByPosition(channel));

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		View v = inflater.inflate(R.layout.news_details, container, false);
		viewpager = (ViewPager) v.findViewById(R.id.news_details_viewpager);
		adapter = new NewsDetailsAdapter(NewsResultHolder.get(channel), getActivity());
		viewpager.setAdapter(adapter);
		viewpager.setCurrentItem(position);
		getActivity().invalidateOptionsMenu();
		return v;

	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
	
	}

	

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int position) {
		if (listener != null){
			listener.onNewsItemChanged(position);
		}
		
	

	}
	
	public void setListener(NewsItemChangedListener listener){
		this.listener = listener;
	}
}
